package cn.com.connext.scrm.web.rest;

import cn.com.connext.scrm.domain.master.ZnhjEntity;
import cn.com.connext.scrm.service.ScanCodeRecordImpl;
import cn.com.connext.scrm.service.dto.AddProductResult;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.dto.ZnhjOperation;
import cn.com.connext.scrm.service.mapper.ZnhjService;
import cn.com.connext.scrm.web.rest.vm.IsScanVM;
import cn.com.connext.scrm.web.rest.vm.ScanCodeRecordVM;
import com.connext.common.web.rest.BaseResource;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 第三方调用接口
 */
@RestController
@RequestMapping("/api/znhj/")
public class ZnhjResource extends BaseResource {
    private final Logger logger = LoggerFactory.getLogger(ZnhjResource.class);
    private static final String author = "Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2MDIzNDM1ODUsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiMSJdLCJqdGkiOiI1M2Y2OWUwMi0wYzczLTRkNjktOTI1YS03N2NmOGQxMjhkMWIiLCJjbGllbnRfaWQiOiJ3ZWJfYXBwIiwic2NvcGUiOlsib3BlbmlkIl19.HpdO_l5Psr4uVsIuHobvqaF6lpWxW-bk83dku2BJzitA-pFO0MKe4rFdS3Ex_E6PydCh-KLbuIOhDxBsrQGX5nnjVmBnVtQ65sOmBXfrItLbH9-mi4rZ7MtPY77y0-5LG6GpgS2v2src16AX6Bk-kExIbd1WD2CkpWOLq8cVpV5AB0sdqK37tn4kd0fB1QPdACiJBoTJifENQ2QHWJgVLQ8GfdtpsZZuPEIM_k1JceRFVP_u1xBFnzUUMwEGsSbc8Zqd30Gzq524k5FdTzPP3UHmKAJCpYF1qcjaisqTBsz0e-eHTunJVwHIfasWHRkIAiQJ4sTTtmyaflD0eMd_aw";

    @Autowired
    private ZnhjService znhjService;

    public ZnhjResource() {
        super("Znhj");
    }
    ReentrantLock lock = new ReentrantLock();
    @Autowired
    private ScanCodeRecordImpl scanCodeRecordImpl;
    /**
     * 获取票据
     */
    @ApiOperation(value = "尊尼获加添加商品与码信息", notes = "尊尼获加添加商品与码信息")
    @PostMapping(value = "/addProduct")
    public AddProductResult addProduct(@Valid @RequestBody ZnhjOperation znhj, HttpServletRequest request ){
        AddProductResult result =new AddProductResult();
        lock.lock();
        // if(znhj.getOperation() ==1){
        //新增
        try{
            result = znhjService.addProducts(znhj);
        }catch (Exception e){
            logger.info("添加------------"+e.getMessage());
        }finally {
            lock.unlock();
        }
        /*}else if(znhj.getOperation() ==2){
            //删除
            try{
                jsonResult = znhjService.deleteProducts(znhj.getList());
            }catch (Exception e){
                logger.info("删除------------"+e.getMessage());
            }finally {
                lock.unlock();
            }
        }else{
            jsonResult.setState(1);
            jsonResult.setMessage("无效操作");
            lock.unlock();
        }*/
        return  result;
    }
    //旧版代码
    /*public JsonResult addProduct(@Valid @RequestBody ZnhjOperation znhj, HttpServletRequest request ){
        JsonResult jsonResult =new JsonResult();
        lock.lock();
        if(znhj.getOperation() ==1){
            //新增
            try{
                jsonResult = znhjService.addProducts(znhj.getList());
            }catch (Exception e){
                logger.info("添加------------"+e.getMessage());
            }finally {
                lock.unlock();
            }
        }else if(znhj.getOperation() ==2){
            //删除
            try{
                jsonResult = znhjService.deleteProducts(znhj.getList());
            }catch (Exception e){
                logger.info("删除------------"+e.getMessage());
            }finally {
                lock.unlock();
            }
        }else{
            jsonResult.setState(1);
            jsonResult.setMessage("无效操作");
            lock.unlock();
        }
        return  jsonResult;
    }*/


    /**
     * 扫码添加次数
     * @param vm
     * @return
     */
    @PostMapping("addScanCodeMsg")
    public JsonResult addScanCodeMsg(@RequestBody ScanCodeRecordVM vm) {
        JsonResult result = new JsonResult();
        try {
            result =scanCodeRecordImpl.addScanCodeMsg(vm);
        } catch (Exception e) {
            logger.error("e:{}", e);
            result.setState(-1);
            result.setMessage("添加失败");
        }
        return result;
    }



    /**
     * 修改sku扫码后的状态isScan
     * @param vm
     * @return
     */
    @PostMapping("updateIsScan")
    public JsonResult updateIsScan(@RequestBody IsScanVM vm) {
        JsonResult result = new JsonResult();
        try {
            //修改码状态
            result =znhjService.updateIsScan(vm);
            //根据用户unionId查询top券信息
        } catch (Exception e) {
            logger.error("e:{}", e);
            result.setState(-1);
            result.setMessage("修改成功");
        }
        return result;
    }


    /**
     * 根据sku统计扫码人数和次数
     * @param vm
     * @return
     */
    @PostMapping("statisticsScanCount")
    public JsonResult statisticsScanCount(@RequestBody ScanCodeRecordVM vm) {
        JsonResult result = new JsonResult();
        try {
            //type =1 //统计次数　　２　统计人数　　 3 被扫描的码数
            result =scanCodeRecordImpl.statisticsScanCount(vm);
            //根据用户unionId查询top券信息
        } catch (Exception e) {
            logger.error("e:{}", e);
            result.setState(-1);
            result.setMessage("添加失败");
        }
        return result;
    }


    /**
     * 统计被扫描的码数
     * @param
     * @return
     */
    @PostMapping("findSkuByBottleCode")
    public JsonResult findSkuByBottleCode(@RequestBody ZnhjEntity entity) {
        JsonResult result = new JsonResult();
        try {
            result =znhjService.findSkuByBottleCode(entity);
            //根据用户unionId查询top券信息
        } catch (Exception e) {
            logger.error("e:{}", e);
            result.setState(-1);
            result.setMessage("添加失败");
        }
        return result;
    }

    @GetMapping("demo")
    public JsonResult addScanCodeMsg() {
        JsonResult result = new JsonResult();
        try {
            Thread.sleep(10000);
            result.setState(0);
            result.setMessage("测试成功");
        } catch (Exception e) {
            logger.error("e:{}", e);
            result.setState(-1);
            result.setMessage("添加失败");
        }
        return result;
    }

    @GetMapping("/getNumByReelCode")
    public ResponseEntity getNumByReelCode(String reelCode){
        return entityResponse(znhjService.getNumByReelCode(reelCode));
    }
}

